package com.ruoyi.material.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateCheck.domain.MateCheck;
import com.ruoyi.mateCheck.mapper.MateCheckMapper;
import com.ruoyi.mateCheck.service.MateCheckService;
import com.ruoyi.material.domain.MateProductCheck;
import com.ruoyi.material.mapper.CheckReportMapper;
import com.ruoyi.material.mapper.MateProductMapper;
import com.ruoyi.material.service.CheckReportService;
import com.ruoyi.material.service.MateProductService;
import com.ruoyi.mtAudit.domain.MtAudit;
import com.ruoyi.mtAudit.mapper.MtAuditMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */
@RestController
@RequestMapping("/checkReport")
public class CheckReportController extends BaseController {
    @Autowired
    MateProductService mateProductService;

    @Autowired
    MateProductMapper mateProductMapper;

    @Autowired
    MateCheckMapper mateCheckMapper;

    @Autowired
    CheckReportService checkReportService;

    @Autowired
    CheckReportMapper checkReportMapper;

    @Autowired
    MateCheckService mateCheckService;

    @Autowired
    MtAuditMapper mtAuditMapper;

    /**
     * 新增保存原材料检验报告
     */
    @Log(title = "检验报告", businessType = BusinessType.INSERT)
    @PostMapping("/addCheck")
    public AjaxResult addCheck(@RequestBody MateProductCheck mateProductCheck) {
        mateProductCheck.setDeptId(SecurityUtils.getLoginUser().getCompanyId());
        int a = checkReportService.addCheck(mateProductCheck);
        if (a > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败");
        }
    }

    /*
    查询已经审核完成的检验记录
     */
    @GetMapping("/selectMateCheck")
    public Map<String, Object> selectMateProductCheck() {
        Map<String, Object> resMap = new HashMap<>();
        List<MateCheck> list = mateCheckService.selectMateProductCheck(SecurityUtils.getDeptId());
        for (MateCheck mateCheck : list) {
            JSONArray jsonArray = new JSONArray(Collections.singletonList(mateCheck.getInformationList()));
            mateCheck.setOtherInformation(jsonArray);
        }
        resMap.put("result", list);
        return resMap;
    }

    /*
    查询已经做完的原材料检验报告
     */
    @GetMapping("/selectCheck")
    public MateProductCheck selectCheck(@RequestParam(value = "mateCheckId", required = false) Long mateCheckId) {
        MateProductCheck mateProductCheck = checkReportMapper.selectChcek(mateCheckId);
        JSONArray jsonArray = new JSONArray(Collections.singletonList(mateProductCheck.getInformationList()));
        mateProductCheck.setOtherInformation(jsonArray);
        return mateProductCheck;
    }

    /**
     * 查询原材料检验报告list页面
     */
    @RequiresPermissions("business:procurement:list")
    @GetMapping("/checklist")
    @ResponseBody
    public TableDataInfo checklist(@RequestParam(value = "beginTime", required = false) String beginTime,
                                   @RequestParam(value = "endTime", required = false) String endTime) {
        MateProductCheck mateProductCheck = new MateProductCheck();
        mateProductCheck.setBeginTime(beginTime);
        mateProductCheck.setEndTime(endTime);
        startPage();
        mateProductCheck.setDeptId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        List<MateProductCheck> list = checkReportService.selectMateProductCheck(mateProductCheck);
        return getDataTable(list);
    }

    /*
    编辑原材料检验报告
     */
    @PostMapping("/editCheck")
    @ResponseBody
    public AjaxResult updateMateProductCheck(@RequestBody MateProductCheck mateProductCheck) {
        int num = checkReportService.updateMateProductCheck(mateProductCheck);
        if (num > 0) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败");
        }
    }

    @GetMapping("/remove")
    public AjaxResult remove(@RequestParam(value = "mateCheckId", required = false) Long mateCheckId,
                             @RequestParam(value = "ckInformationId", required = false) Long ckInformationId) {
        checkReportService.remove(mateCheckId, ckInformationId);
        return AjaxResult.success("删除成功");
    }

    @GetMapping("/selectAuditReport")
    @ResponseBody
    public Map<String, Object> selectAuditReport() {
        Map<String, Object> resMap = new HashMap<>();
        List<MateProductCheck> list = checkReportService.selectAuditReport(SecurityUtils.getDeptId());
        resMap.put("result", list);
        return resMap;
    }

}
